<template>
	<view class="page">
		<u-tabs :list="cateList"  :current="current" lineColor="#23c993" keyName="title" lineWidth="30" :activeStyle="{color: '#23c993',fontWeight: 'bold',transform: 'scale(1.05)'}"></u-tabs>
		
		<u-button text="开通" @click="open"></u-button>
	</view>
</template>

<script>
	import config from '@/common/config'
	export default {
		data() {
			return {
				current: 0,
				cateList:[
					{
						id:1,
						title: '月卡会员'
					},
					{
						id:2,
						title: '年卡会员'
					}
				]
			};
		},
		onShow(){
			if(!uni.getStorageSync('LOGIN_TOKEN_KEY')){
				uni.navigateTo({
					url: "/pages/login/login"
				});
				return;
			}
			
		},
		onLoad(e){
			if(e&&e.current){
				this.current = e.current;
			}
			
		},
		methods: {
			async wxpay(options){
				return new Promise((resolve, reject) => {
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: options.timeStamp,
						// 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
						nonceStr: options.nonceStr,
						// 支付签名随机串，不长于 32 位
						package: options.package,
						// 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
						signType: options.signType,
						// 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
						paySign: options.paySign,
						success: res => resolve(res),
						cancel: res => reject(res),
						fail: res => reject(res),
					})
				})
			},
			handleWechatPay(data) {
				return new Promise((resolve, reject) => {
					this.wxpay(data)
						.then(async (res) => {
							resolve(res)
			
						})
						.catch(errMsg => reject(errMsg))
				})
			},
			open(){
				var type = 1;
				if(this.current == 1){
					type = 2;
				}
				uni.$u.http.post('/Order/create',{type}).then(async res =>{
					let {order_id,from} = res;
					uni.$u.http.post('/Pay/prepay',{
						from: from,
						order_id: order_id,
					}).then(async ({
						config,
						pay_way,
						paid
					}) => {
						if(paid && paid == 1){
							uni.$u.toast("支付成功");
							return ;
						}
						this.handleWechatPay(config).then(async () => {
							// this.handlePayResult(PaymentStatusEnum['SUCCESS'])
							uni.$u.toast("支付成功");
							setTimeout(() => {
								uni.redirectTo({
									url: "/pages/my/myOrder"
								})
							}, 500)
						}).catch(errMsg => {
							console.log(errMsg, '---');
							uni.$u.toast("支付失败");
						})
					})
				}).catch(err => {
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.page{
	padding: 30rpx;
}
.unit{
	display: flex;
	align-items: center;
	text{
		margin-right: 10rpx;
	}
}
</style>
